﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.TabPageControls
{
    public partial class QuanLyNCC : UserControl
    {
        public QuanLyNCC()
        {
            InitializeComponent();
        }

        private void DanhSachNCC_Load(object sender, EventArgs e)
        {
            loadData();
        }

        private void loadData()
        {
            dgvNhaCungCap.Rows.Clear();

            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            var NhaCungCaps = from s in dc.NhaCungCaps where s.deleted == false select s;
            int count = 1;
            foreach (_DB.NhaCungCap s in NhaCungCaps)
            {
                DataGridViewRow row = new DataGridViewRow();
                row.CreateCells(dgvNhaCungCap);
                row.Cells[0].Value = count++;
                row.Cells[1].Value = s.MaNhaCungCap;
                row.Cells[2].Value = s.TenNhaCungCap;

                dgvNhaCungCap.Rows.Add(row);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dgvNhaCungCap.SelectedRows.Count > 0)
            {
                string maNCC = (string) dgvNhaCungCap.SelectedRows[0].Cells[1].Value;
                OtherForms.ThemNCCForm f = new OtherForms.ThemNCCForm(maNCC);
                f.ShowDialog();
                loadData();
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            
            if (MessageBox.Show("Are you sure?", "Confirm", MessageBoxButtons.YesNo) != DialogResult.Yes)
                return;

            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();

            foreach (DataGridViewRow row in dgvNhaCungCap.SelectedRows)
            {
                String ID = (String)row.Cells[1].Value;

                _DB.NhaCungCap sup = (from s in dc.NhaCungCaps
                                    where (s.MaNhaCungCap == ID)
                                    select s).Single();
                sup.deleted = true;
            }

            dc.SubmitChanges();
            loadData();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Main m = (Main)this.Tag;
            m.closeTab();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            OtherForms.ThemNCCForm f = new OtherForms.ThemNCCForm();
            f.ShowDialog();
            loadData();
        }

        private void dgvNhaCungCap_DoubleClick(object sender, EventArgs e)
        {
            if (dgvNhaCungCap.SelectedRows.Count > 0)
            {
                string maNCC = (string)dgvNhaCungCap.SelectedRows[0].Cells[1].Value;
                OtherForms.XemNCCForm f = new OtherForms.XemNCCForm(maNCC);
                f.Show();
            }
        }

        private void btnReport_Click(object sender, EventArgs e)
        {
            Reports.DS_DanhSachNhaCungCap ds = new Reports.DS_DanhSachNhaCungCap();
            ds.fillData();
            ds.viewReport();
        }
    }
}
